package ru.usu.gv.gui.analysis;

import java.util.List;

import javax.vecmath.Vector3d;

import ru.usu.gv.core.GraphCanvas;
import ru.usu.gv.uicomponents.UIEdge;
import ru.usu.gv.uicomponents.UIGraph;

/**
 @author Sergey Pupyrev 
 13.04.2007
 */

public class OrtogonalAnalyzer extends GraphAnalyzer
{

    public OrtogonalAnalyzer()
    {
        super(AnalysisPanel.getInstance().getOrtogonalSlider());
    }

    @Override
    protected double calculateAnalysValue()
    {
        UIGraph uiGraph = GraphCanvas.getInstance().getCurrentGraph();

        double error = 0;
        List<UIEdge> edgeList = uiGraph.getEdges();
        for (UIEdge edge : edgeList)
        {
            double angleX = calculateAngle(edge, new Vector3d(1.0, 0.0, 0.0));
            double angleY = calculateAngle(edge, new Vector3d(0.0, 1.0, 0.0));
            double angleZ = calculateAngle(edge, new Vector3d(0.0, 0.0, 1.0));

            double angle = Math.min(angleX, angleY);
            angle = Math.min(angle, angleZ);
            error += 2.0 * angle / Math.PI;
        }

        return 1.0 - error / edgeList.size();
    }

    private double calculateAngle(UIEdge edge, Vector3d axis)
    {
        Vector3d edgeVector = new Vector3d(edge.getV2().getPosition());
        edgeVector.sub(edge.getV1().getPosition());
        return axis.angle(edgeVector);
    }

}
